package cn.ztc.plugin.main.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.ztc.plugin.ext.ResourcesExt;
import cn.ztc.plugin.main.entity.Resources;
import cn.ztc.plugin.ext.ResourceFilterExtApi;
import cn.ztc.plugin.main.mapper.ResourcesMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gitee.starblues.factory.process.pipe.extract.ExtractFactory;
import com.gitee.starblues.factory.process.pipe.extract.ExtractCoordinate;

import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ZhangTianCi
 */
@RestController
@RequestMapping("resources")
public class ResourcesController {
    @Resource
    ResourcesMapper resourcesMapper;
    private final ExtractFactory extractFactory = ExtractFactory.getInstant();

    @RequestMapping("list")
    public List<Resources> list() {
        List<Resources> resources = resourcesMapper.selectList(null);
        // TODO: 根据Class获取暂未成功.应该是开源方改代码的时候改错了
        // https://gitee.com/starblues/springboot-plugin-framework-parent/issues/I44PLQ
        //        List<ResourceFilterExtApi> extractByInterClass = extractFactory.getExtractByInterClass(ResourceFilterExtApi.class);
        //        ResourceFilterExtApi resourceFilterExtApi = extractByInterClass.stream().findAny().orElse(null);
        ResourceFilterExtApi resourceFilterExtApi = extractFactory.getExtractByCoordinate(ExtractCoordinate.build("ResourceFilterExtApi"));
        if (Objects.nonNull(resourceFilterExtApi)) {
            List<ResourcesExt> filterResult = resourceFilterExtApi.filter(
                    resources.stream().map(t -> BeanUtil.copyProperties(t, ResourcesExt.class)).collect(Collectors.toList()), 1);
            return filterResult.stream().map(t -> BeanUtil.copyProperties(t, Resources.class)).collect(Collectors.toList());
        }
        return resources;
    }
}
